<?php
class UserController extends Controller
{
	/**
	 * 用户个人首页
	 */
	public function show()
	{
		$uid	=	fetch_get('uid','int');
		if(!$uid || !($user=User::get($uid)))
		{
			$this->redirect();
		}
		$this->assign('user', $user);
		
		//取用户发过的谈论
		$talks	=	TalkHelper::listForUser(MODEL_ARTICLE, $uid );
		ArticleHelper::patch($talks);
		$this->assign('talks', $talks);

		$this->display();
	}
	
	/**
	 * 用户登陆页面
	 */
	public function login()
	{
		if(!empty($_POST))
		{
			$username	=	strtolower(fetch_post('username'));
			$password	=	fetch_post('password', 'char');
			if($username && $password)
			{
				if(UserAuthHelper::doLogin($username, $password))
				{
					if(fetch_get('r')){
						header('location: '.base64_decode(fetch_get('r')));
					}
					elseif(fetch_post('referer')){
						header('location: '.fetch_post('referer'));
					}
					else{
						$this->redirect();
					}
				}
				else
				{
					Show::warning('登陆失败，用户名或密码错误', 'SELF');
				}
			}
			else
			{
				Show::warning('输入用户名和密码', 'SELF');
			}
		}

		$this->display();
	}
	
	public function logout()
	{
		UserAuthHelper::doLogout();
		
		if(fetch_get('r')){
			header('location: '.base64_decode(fetch_get('r')));
		}
		elseif(!empty($_SERVER['HTTP_REFERER'])){
			header('location: '.$_SERVER['HTTP_REFERER']);
		}
		else{
			$this->redirect();
		}
	}
	
	/**
	 * 用户注册页面
	 *
	 */
	public function reg()
	{
		if(!empty($_POST))
		{
			$username	=	strtolower(fetch_post('username'));
			$password	=	fetch_post('password', 'char');
			$passwd2	=	fetch_post('password2', 'char');
			$nickname	=	fetch_post('name');
			$vacode		=	strtolower(fetch_post('va'));
			
			if(!$username)
			{
				Show::warning('请输入登陆用户名');
			}
			elseif(!StringHelper::email($username))
			{
				Show::warning('请输入正确有效的邮箱做为用户名');
			}
			if(!$password || $password!=$passwd2)
			{
				Show::warning('您的登陆密码不能留空，且两次密码输入要一致');
			}
			if(!$vacode)
			{
				Show::warning('请输入图片上的验证码');
			}
			/*
			elseif(0 && $vacode==$_SESSION['VACODE'])
			{
				Show::warning('验证码输入有误');
			}*/
			
			$uid	=	User::add(array(
							'username'	=>	$username,
							'password'	=>	UserAuthHelper::encrypt($password),
							'email'		=>	$username,
							'name'		=>	$nickname,
							'regtime'	=>	time(),
						));
			if($uid>0)
			{
				Show::notice('恭喜注册成功', 'user-login/u/'.$username, 3);
			}
			else
			{
				Show::warning('注册失败，请稍候再试', WEB_ROOT, 3);
			}
		}
		
		$this->display();
	}
	
	public function changepass()
	{
		if(isset($_POST['submit2']))
		{
			$r	=	self::changePasswd(fetch_post('password1', 'char'), fetch_post('password2', 'char'));
			switch($r)
			{
			case  0:	$message	=	"密码修改成功";	break;
			case -1:	$message	=	"新密码不能为空";	break;
			case -2:	$message	=	"原始密码错误";	break;
			case -3:	$message	=	"修改操作失败";	break;
			case -4:	$message	=	"两次密码输入不一致";	break;
			}
			$this->assign('errmsg',	$message);
		}

		$this->display();
	}
	
	public function info()
	{
		if(UserAuthHelper::isLogin())
			$this->display();
		else
			$this->redirect();
	}
	
	//-----------------------------PRIVATE METHOD-------------------------------
	
	/** 修改个人密码   */
	private static function changePasswd($passwd1, $passwd2, $uid=0)
	{
		if(empty($passwd1))
		{
			return -1;
		}
		$user	=	Adminuser::get($uid ? $uid : $_SESSION['USER']['id']);
//		if(self::encrypt($oldpasswd) != $user['password'])
//		{
//			return -2;
//		}
		if($passwd1!=$passwd2)
		{
			return -4;
		}
		if(!Adminuser::changePasswd($user['id'], UserAuthHelper::encrypt($passwd1)))
		{
			return -3;
		}
		return 0;
	}
	
}
?>